package myJs.api

import japgolly.scalajs.react.extra.Ajax
import myJs.Implicits._
import myJs.myPkg.MyFetch
import org.scalajs.macrotaskexecutor.MacrotaskExecutor.Implicits._
import upickle.default._

/** Created by yz on 18/10/2022
  */
object VarInfoFileData {

  import shared.pojo.VarInfoFileData._

  val baseUrl = s"${Api.apiUrl}/user/varInfoFileData"

  def getAll(request: GetAll.Request) = {
    val url =
      s"${baseUrl}/getAll?groupKind=${request.groupKind}&columnNumLeast=${request.columnNumLeast}&fileName=${request.fileName}"
    Ajax
      .get(s"${url}")
      .setRequestContentTypeJson
      .send()
      .asAsyncCallback
      .map { xhr =>
        read[List[ujson.Obj]](xhr.responseText)
      }
  }

  def getGroups(request: GetGroups.Request) = {
    val url = s"${baseUrl}/getGroups?file=${request.file.encodeURI}&groupFile=${request.groupFile.encodeURI}"
    Ajax
      .get(s"${url}")
      .setRequestContentTypeJson
      .send
      .asAsyncCallback
      .map { xhr =>
        read[List[String]](xhr.responseText)
      }
  }

  def isPaired(request: GetGroups.Request) = {
    val url = s"${baseUrl}/isPaired"
    MyFetch
      .getRnStr(
        url = s"${url}?groupFile=${request.groupFile}&file=${request.file}",
        headers = Map("Content-Type" -> "application/json")
      )
      .map { str =>
        read[Boolean](str)
      }
  }

}
